﻿using System.Dynamic;
using Basic.FormCollect;
using Basic.FormModel.DB;
using Basic.FormRemoteModel.FormView.Model;
using Basic.FormService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Model;

namespace Basic.FormService.lmpl
{
    internal class FormViewService : IFormViewService
    {
        private readonly IFormViewCollect _FormView;
        private readonly IAgentFormCollect _AgentForm;
        private readonly ICustomFormCollect _Form;
        public FormViewService ( IFormViewCollect formView,
            ICustomFormCollect from,
            IAgentFormCollect agentForm )
        {
            this._Form = from;
            this._AgentForm = agentForm;
            this._FormView = formView;
        }


        public long Add ( FormViewAdd data )
        {
            return this._FormView.Add(data);
        }
        public void Delete ( long id )
        {
            DBFormView sour = this._FormView.Get(id);
            string name = this._Form.Get(sour.FormId, a => a.TableName);
            this._FormView.Delete(sour, name);
        }
        public bool Set ( long id, FormViewSet set )
        {
            DBFormView sour = this._FormView.Get(id);
            return this._FormView.Update(sour, set);
        }

        public FormViewDto Find ( long formId )
        {
            DBFormView sour = this._FormView.Find(formId);
            if ( sour != null )
            {
                return sour.ConvertMap<DBFormView, FormViewDto>();
            }
            return null;
        }
        public PagingResult<ExpandoObject> Query ( long formId, IBasicPage basicPage )
        {
            string name = this._Form.Get(formId, a => a.TableName);
            ExpandoObject[] list = this._AgentForm.Query(name, basicPage, out int count);
            return new PagingResult<ExpandoObject>(count, list);
        }
    }
}
